import { login, logout, getUserInfo, reg, qqCheck, refreshToken, wxCheck } from '@/api/login'
import { getToken, setToken, removeToken, removePath, getWechatLoginStatus , saveStorage, removeStorage } from '@/utils/auth'
import { Base64 } from '@/utils/xchat/base64'
const user = {
  state: {
    token: getToken(),
		user:undefined,
		logined:false,
		wechatLoginStatus:getWechatLoginStatus()
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER: (state, user) => {
      state.user = user
    },
		SET_WECHAT_LOGIN_STATUS:(state,status)=>{
			state.wechatLoginStatus = status
		}
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
					if(response.data.wx_openId){
						saveStorage('openid',response.data.wx_openid)
					}
					commit('SET_USER','')
					commit('SET_TOKEN', response.data.token)
					removeToken()
					setToken(response.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
		ThirdPartLogin({ commit },{parsedUrl,type}){
			return new Promise((resolve, reject) => {
				if(type === 'qq'){
					qqCheck(parsedUrl).then(response=>{
						commit('SET_USER','')
						commit('SET_TOKEN', response.data.token)
						removeToken()
						setToken(response.data.token)
						resolve(response)
					}).catch(error=>{
						reject(error)
					})
				}else if(type === 'wx'){
					wxCheck(parsedUrl).then(response=>{
						if(response.data.open_id){
							saveStorage('openid',response.data.open_id)
						}
						commit('SET_USER','')
						commit('SET_TOKEN', response.data.token)
						removeToken()
						setToken(response.data.token)
						resolve(response)
					}).catch(error=>{
						reject(error)
					})
				}
			})
		},
		Reg({commit},regInfo){
			return new Promise((resolve, reject) => {
			  reg(regInfo).then(response => {
					commit('SET_USER','')
					commit('SET_TOKEN', response.data.token)
					removeToken()
					setToken(response.data.token)
			    resolve(response)
			  }).catch(error => {
			    reject(error)
			  })
			})
		},
    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
 					if(response.data.user.token){
 						commit('SET_TOKEN',response.data.user.token)
						setToken(response.data.user.token)
 					}
					if(response.data.user && response.data.user.logined && 'nickname' in response.data.user){
						response.data.user.nickname = Base64.decode(response.data.user.nickname)
					}
					commit('SET_USER',response.data.user)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
					removeStorage('openid')
          commit('SET_TOKEN', '')
          removeToken()
					removePath()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
		
		//刷新token
		Refresh({ commit }) {
		  return new Promise((resolve, reject) => {
		    refreshToken().then(response => {
					commit('SET_TOKEN', response.data)
					removeToken()
					setToken(response.data)
		      resolve()
		    }).catch(error => {
		      reject(error)
		    })
		  })
		}
  }
}

export default user
